package com.woniuxy.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.entity.Perms;
import com.woniuxy.mapper.PermMapper;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.Map;

public class PermMapFactoryBean implements FactoryBean<Map<String,String>> {

    @Autowired
    PermMapper permMapper;

    @Override
    public Map<String, String> getObject() throws Exception {
        //配置请求的url与过滤器的关系
        Map<String,String> map=new LinkedHashMap<>();
        map.put("auth","anon");
        map.put("/user/userLogin","anon");
        map.put("/user/*","roles[user]");
        map.put("/menu/get","jwt");

        //查询所有的权限列表信息
        QueryWrapper<Perms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type","a");
        queryWrapper.eq("status","y");
        permMapper.selectList(queryWrapper).forEach(p->{
            map.put("/"+p.getLink(),"jwt,perms["+p.getCode()+"]");
        });

        return map;
    }

    @Override
    public Class<?> getObjectType() {
        return Map.class;
    }
}
